package uk.ac.aber.paws.client.state;

import java.io.File;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;

import uk.ac.aber.paws.client.core.Initiator;
import uk.ac.aber.paws.client.core.PAWSClient;
import uk.ac.aber.paws.client.db.DBCall;
import uk.ac.aber.paws.client.idlelogger.IdleLogger;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * SystemState.java
 * 
 * This class stores all application data relating to the session in progress
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class SystemState {

	
	/**
	 * The database ID for the current login session
	 */
	private long sessionID = -1l;
	
	private ComputerInfo computerInfo;
	
	private PowerEventInfo powerEventInfo;
	
	//private LoginInfo loginInfo;
	
	private LoginRegister loginRegister;
	
	/**
	 * The last time a user logged in - in milliseconds
	 */
	private long lastUserLoginTime = Calendar.getInstance().getTimeInMillis();
		
	private Hashtable<String,Long> userIdleTime = new Hashtable<String,Long>();
	
	private long serviceStateTime;
	
	private String language;
	
	private Vector<DBCall> cachedDBcalls;
	
	public SystemState() {

		cachedDBcalls = new Vector<DBCall>();
		
		computerInfo = new ComputerInfo();
		powerEventInfo = new PowerEventInfo();
		
		//loginInfo = new LoginInfo();
		
		serviceStateTime = Calendar.getInstance().getTimeInMillis();
		
		initialiseLanguage();
		
	}
	
	public void initialiseLoginRegister() {
	
		loginRegister = new LoginRegister();
	}
	
	public String getLanguage() {
		
		return language;
	}
	
	private void initialiseLanguage() {
		
		// Handle multiple languages
		// if a user has logged in with welsh, then the following file will exist showing that the
		// login is in a welsh environment
		if ((new File(PAWSClient.getConfig().getProperty("app.os.config.language.welshid"))).exists()) {
			Initiator.LOG_FILE.log(Level.INFO,"Language Preference = Welsh");
			language = PAWSClient.getConfig().getProperty("app.language.key.welsh");
		}
				
		// default is english language
		else {
			Initiator.LOG_FILE.log(Level.INFO,"Language Preference = English");
			language = PAWSClient.getConfig().getProperty("app.language.key.english");
		}
	}

	public void setUserIdleTime(long time, String userName) {
		
		userIdleTime.put(userName, time);
	}
	
	public void clearUserIdleTimes() {
		
		userIdleTime.clear();
	}
	
	public Hashtable<String,Object> exportData() {
	
		Hashtable<String,Object> dataHash = new Hashtable<String,Object>();
		
		dataHash.put("COMPUTER_NAME",computerInfo.getComputerName());
		dataHash.put("DOMAIN",computerInfo.getDomain());
		dataHash.put("LAST_SLEEP_TIME",powerEventInfo.getLastSleep());
		dataHash.put("LAST_WAKE_TIME",powerEventInfo.getLastWake());
		dataHash.put("LAST_IDLE",getUserIdleTime());
		dataHash.put("OPERATING_SYSTEM",computerInfo.getOperatingSystem());
		dataHash.put("SERIAL_NUMBER",computerInfo.getSerialNumber());
		dataHash.put("PAWS_VERSION",computerInfo.getPawsVersion());
		dataHash.put("PAWS_CLIENT_STARTIME",serviceStateTime);
		
		return dataHash;
	}
	
	public Hashtable<String,Long> getUserIdleTime() {
		
		return userIdleTime;
	}

	public void addCachedDBCall(DBCall call) {
		cachedDBcalls.add(call);
	}
	
	public void removeCachedDBCall(int index) {
		cachedDBcalls.remove(index);
	}
	
	public Vector<DBCall> getCachedDBcalls() {
		return cachedDBcalls;
	}
	
	/**
	 * Sets the specified time for the last suer login
	 * @param time The time in millisecs of the last user login
	 */
	public void setLastUserLoginTime(long time) {
		
		lastUserLoginTime = time;
	}
	
	/**
	 * @return Returns the milliseconds of the last user login 
	 */
	public long getLastUserLoginTime() {
		
		return lastUserLoginTime;
	}

	
	/**
	 * Sets the database derived session ID for the new login session
	 * @param sessionID The integer index for the login session
	 */
	public void setSessionID(long sessionID) {
		this.sessionID = sessionID;
	}
	
	/**
	 * 
	 * @return Returns the session ID for the current login session
	 */
	public long getSessionID() {
		return this.sessionID;
	}

	
	public ComputerInfo getComputerInfo() {
		return computerInfo;
	}
	
	public PowerEventInfo getPowerEventInfo() {
		return powerEventInfo;
	}
	
	public LoginRegister getLoginRegister() {
		
		return loginRegister;
	}
}
